// pages/evaluateevent/evaluateevent.js
const {
  $Message
} = require('../../iview/base/index');

var app = getApp();
const db = wx.cloud.database()
const user = db.collection("user")
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  /* 这一页需要传进来的参数是活动的id */
  onLoad: function(options) {
    this.setData({
      id: options.id
    })
    var that = this
    const db = wx.cloud.database()
    const eventco = db.collection("event")
    eventco.where({
      _id: options.id
    }).get().then(function(res) {
      that.setData({
        event: res.data[0]
      })
    })

  },

  inputChange(e) {
    var that = this
    switch (e.currentTarget.id) {
      case "comment":
        {
          that.setData({
            comment: e.detail.value
          })
        }
        break
      case "score":
        {
          that.setData({
            score: e.detail.value
          })
        }
        break
    }
  },

  commit: function(e) {
    var that = this
    const db = wx.cloud.database()
    const eventco = db.collection("event")
    if (this.data.comment != undefined && this.data.score != undefined) {
      if (that.data.score > 5) {
        $Message({
          content: '活动评分不能超过5分',
          type: "default"
        })
      } else {
        var temp
        eventco.where({
          _id: that.data.id
        }).get().then(function(oldinfo){
          temp=oldinfo.data[0].feedback
          if(temp==undefined){
            temp=[{
              participant:app.globalData.openID,
              comment:that.data.comment,
              score:parseInt(that.data.score)
            }]
          }
          else{
            temp.push({
              participant: app.globalData.openID,
              comment: that.data.comment,
              score:parseInt(that.data.score)
            })
          }
        }).then(function(newinfo){
          wx.cloud.callFunction({
            name: "eventFeedback",
            data: {
              id: that.data.id,
              feed:temp
            }
          }).then(function (e) {
            // user.get().then(res=>{
            //   console.log(res.data)
            //   for(var i=0;i<res.data.myevents.length;i++){
            //     if (res.data.myevents[i].event_id==this.data.id){
            //       res.data.myevents[i].score = temp.score
            //       user.doc(res.data._id).update({
            //         myevents: res.data.myevents
            //       })
            //     }
            //   }              
            // })
            console.log(e)
            $Message({
              content: '评价成功',
              type: "success"
            })
            wx.navigateBack({
              delta: 1,
            })
          }).catch(function (err) {
            console.log(err)
            $Message({
              content: '评价失败，请联系开发者',
              type: 'error'
            })
          })  
        })
        
      }
    } else {
      $Message({
        content: '请填写完整信息喔~',
        type: "defalut"
      });
    }
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})